package com.homegoods.classify.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.classify.presenter.ClassifyListFragmentPresenter;
import com.homegoods.goods.item.GoodsItem;
import com.homegoods.home.R;
import com.homegoods.home.databinding.FragmentClassifyListLayoutBinding;
import com.homegoods.home.item.ClassifyGoodsGroupItem;
import com.homegoods.home.model.HomeGroupsModel;
import com.homegoods.home.model.SubHomeClassModel;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.util.Utils;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.fragment.KRefreshListFragment;
import com.kedll.library.util.MyUtils;


/**
 * @author QiQi
 * @description 显示分类数据列表
 */
@Route(path = ARouterPath.PATH_HOME_FRAGMENT_CLASSIFY)
public class ClassifyListActivityFragment extends KRefreshListFragment<FragmentClassifyListLayoutBinding, ClassifyListFragmentPresenter> {

    public HomeGroupsModel.ChildrenBean child;

    @Override
    protected ClassifyListFragmentPresenter getPresenter() {
        Bundle bundle = getArguments();
        assert bundle != null;
        child = (HomeGroupsModel.ChildrenBean) bundle.getSerializable("child");
        assert child != null;
        return new ClassifyListFragmentPresenter(this, String.valueOf(child.getId()));
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_classify_list_layout;
    }

    @Override
    protected void init() {
        super.init();
        ARouter.getInstance().inject(this);
        list.setClipToPadding(false);
        list.setPadding(0, MyUtils.dp2px(8), 0, 0);

    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        super.initEvent(savedInstanceState);
        list.addItemDecoration(new SpacesItemDecoration(2, 8));
        list.getItemAnimator().setChangeDuration(0);
        list.setHasFixedSize(true);
    }

    //    @Override
//    protected RecyclerView.LayoutManager getLayoutManager() {
//        return Utils.getGridLayoutManager(getContext(), adapter, 2);
//    }
    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return getGridLayoutManager();
    }

    @NonNull
    private RecyclerView.LayoutManager getGridLayoutManager() {
        GridLayoutManager gridLayoutManager = Utils.getGridLayoutManager(getContext(), adapter, 2);
        final GridLayoutManager.SpanSizeLookup spanSizeLookupHolder = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            //计算每个Item所占的跨度
            @Override
            public int getSpanSize(int position) {
                if (adapter.getItem(position) instanceof ClassifyGoodsGroupItem) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookupHolder != null) {
                    return spanSizeLookupHolder.getSpanSize(position - list.getHeaderItemCount());
                }
                return 1;
            }
        });
        return gridLayoutManager;
    }

    private class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        private int itemCount;

        public SpacesItemDecoration(int itemCount, int space) {
            this.itemCount = itemCount;
            this.space = MyUtils.dp2px(space);

        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            MultiTypeAdapter.IItem iItem = adapter.getItem(position);
            if (iItem instanceof ClassifyGoodsGroupItem) {
                outRect.set(0, 0, 0, 0);
            } else if (iItem instanceof GoodsItem) {
                outRect.bottom = space;
                //当前是推荐类商品，获取位置时，要除去购物车里的商品数量
                position = parent.getChildLayoutPosition(view) - list.getHeaderItemCount();
                if (position % itemCount == 0) {
                    outRect.left = 0;
                    outRect.right = space / 2;
                } else if (position % itemCount == itemCount - 1) {
                    outRect.right = 0;
                    outRect.left = space / 2;
                } else {
                    outRect.right = space / 2;
                    outRect.left = space / 2;
                }
            }
        }
    }
}
